package com.jxw.cidiansdkdemo;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class TBTLActivity extends AppCompatActivity implements JwarWrapper.OnVerify ,Constant {

    TextView bookName, searcr_result;
    boolean isWord; //选择是否是单词
    JwarWrapper mJwarWrapper;
    boolean isBind; //服务是否绑定成功

    //是否打开
    boolean isOpen;
    String book_path;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tbtl);
        bookName = findViewById(R.id.bookName);
        searcr_result = findViewById(R.id.searcr_result);

        Intent intent = getIntent();
        isWord = intent.getBooleanExtra("isWord",false);
        book_path = intent.getStringExtra("book_path");
        bookName.setText(new File(book_path).getName());

        mJwarWrapper = new JwarWrapper(this);
        mJwarWrapper.start(this);
    }


    int unit_index = 0;
    List<String> pLists = new ArrayList<>();
    /**
     * 同步听力数据
     * @param view
     */
    public void onClick(View view){
        boolean isOdd =true;
        int ipage = 0;
        int page = 10; //课本真实标注页码 demo为课本第10页数据
        switch (view.getId()){
            case R.id.unitlist:
                pLists.clear();
                String unitList = mJwarWrapper.getKBUnits(isWord);
                Log.e("zzj","unitList="+unitList);
                StringBuffer sBuffer = new StringBuffer();
                try {
                    JSONArray unitArray = new JSONArray(unitList);
                    for (int i = 0; i < unitArray.length(); i++) {
                       JSONObject unitJson = unitArray.getJSONObject(i);
                       sBuffer.append(unitJson.optString("name")+"\n");
                       pLists.add(unitJson.optJSONArray("list").toString());
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                unit_index = 0;
                searcr_result.setText(sBuffer.toString());
                break;
            case R.id.unitContent://获取单元信息
                try {
                    String wordContent = mJwarWrapper.getUnitContent(pLists.get(unit_index),isWord);
                    searcr_result.setText(wordContent);
                    unit_index++;  //每点击一次单元下标+1
                    if (unit_index == pLists.size()) {
                        unit_index = 0;
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
                break;
            case R.id.unithead:  //20220812新增词头数据接口
                String book_path = "/sdcard/bin/WY版海南国际旅游岛少儿英语一年级上册JWLD_ISBN9787521309539.bin";
                String wordContent = mJwarWrapper.JxwBookInfoToJsonStr(book_path);
                searcr_result.setText(wordContent);
                Log.e("zzj","wordContent="+wordContent);
                break;
        }
    }

    @Override
    public void onError(boolean success) {
        Log.e("zzj", "onError:" + success);
        isBind = success;
        if(isBind){
            //打开选择的课本数据
            mJwarWrapper.openKBFile(book_path);
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        //退出时关闭课本数据
        mJwarWrapper.closeKBFile();

    }
}
